package io.mya.common.utils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class SignUtil {
	
	//对外Key
	private final static String key = "Zpuj8xfvb8jltBkcDvALFcEtWvgXGdxj";
	
	public static void main(String[] args) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("code", "89717EC914B96PF0N7S8");
		params.put("key", "Zpuj8xfvb8jltBkcDvALFcEtWvgXGdxj");
		params.put("timestamp", "1442198292");
		params.put("nonceStr", "TWSm66");
		System.out.println(SignUtil.getSignature(params,"0"));


	}
	static void test(){
		
	}

	/*
	 * 签名生成算法
	 * 
	 * @param HashMap<String,String> params 请求参数集，所有参数必须已转换为字符串类型，值已进行encode
	 * 
	 * @param String secret 后端应用签名密钥 secret
	 * 
	 * @return 签名
	 * 
	 * @throws IOException
	 */
	public static String getSignature(HashMap<String, String> params,
			String secret) {
		if(secret.equals("1")){
			params.put("key", key);
		}
		// 先将参数以其参数名的字典序升序进行排序
		Map<String, String> sortedParams = new TreeMap<String, String>(params);
		Set<Map.Entry<String, String>> entrys = sortedParams.entrySet();
		
		// 遍历排序后的字典，将所有参数按"key=value"格式拼接在一起
		StringBuilder basestring = new StringBuilder();
		for (Map.Entry<String, String> param : entrys) {
			if(!param.getKey().equals("sign")){
				basestring.append(param.getKey()).append("=")
				.append(param.getValue()).append("&");
			}
		}
		
//		if(secret.equals("1")){
//			basestring.delete(basestring.length() - 1, basestring.length()).append("&")
//			.append("key").append("=").append(key);
//		}
		System.out.println(basestring);
		// 使用MD5对待签名串求签
//		byte[] bytes;
//		try {
//			MessageDigest md5 = MessageDigest.getInstance("MD5");
//			bytes = md5.digest(basestring.toString().getBytes("UTF-8"));
//		} catch (GeneralSecurityException ex) {
//			throw new Exception(ex);
//		}

		// 将MD5输出的二进制结果转换为小写的十六进制
//		StringBuilder sign = new StringBuilder();
//		for (int i = 0; i < bytes.length; i++) {
//			String hex = Integer.toHexString(bytes[i] & 0xFF);
//			if (hex.length() == 1) {
//				sign.append("0");
//			}
//			sign.append(hex);
//		}
		return SpecialUtil.toMD5(basestring.toString()).toUpperCase();
	}
	
	public static boolean validSign(HashMap<String, String> params){
		String sign = SignUtil.getSignature(params, "1");
		if(sign.equals(params.get("sign"))){
			return true;
		}
		return false;
	}
	
	/**
	 * 
	 * @param obj
	 * @return
	 */
	public static HashMap<String, String> transBean2Map(Object obj) {
		if (obj == null) {
			return null;
		}
		HashMap<String, String> map = new HashMap<String, String>();
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			for (PropertyDescriptor property : propertyDescriptors) {
				String key = property.getName();
				// 过滤class属性
				if (!key.equals("class")) {
					// 得到property对应的getter方法
					Method getter = property.getReadMethod();
					Object value = getter.invoke(obj);
					map.put(key, value.toString());
				}

			}
		} catch (Exception e) {
			System.out.println("transBean2Map Error " + e);
		}
		return map;

	}
	
}
